import java.sql.*;
public class OpenConnection {

private Connection myConnection;	

public void openConn()
{
	String urlString = "jdbc:mysql://localhost/test";
	String userNameString = "root";
	String pwdString = "saineha85";
	
	try{
		
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		myConnection = DriverManager.getConnection(urlString, userNameString, pwdString);
		//System.out.println("Connection opened successfully in OpenConnection class");
		}
	
	catch (Exception e)
		{
		System.out.println(e);
		}
}

public int Update(String statement1)//throws SQLException 
	{
	int returnInt= 0;
		try
		{
			openConn();
			Statement state1 = myConnection.createStatement();
			returnInt = state1.executeUpdate(statement1);
			state1.close();
			closeConnection();
			return (returnInt);
		}
	
		catch (Exception e)
		{
			System.out.println(e);
		}
		return (returnInt);
	}	

//public void executeQuery(String query) throws SQLException
public ResultSet executeQuery(String query) throws SQLException
	{
		openConn();
		Statement state1 = myConnection.createStatement();
		state1.executeQuery(query);
		ResultSet productResultSet = state1.getResultSet();
	    //state1.close();
	    //myConnection.close();
		return productResultSet;
	}


public void closeConnection()
{
	try{
		myConnection.close();	
		}
	
	catch(Exception e)
	{
		System.out.println(e);
	}
}
}